//
// Created by 11510 on 2024/6/11.
//
#include <string.h>
#include <iostream>
using namespace std;

class CharList{
private:
    char * _data;
    size_t _capacity;

public:
    CharList(const char *str){
        _capacity = strlen(str) + 1;
        _data = new char[_capacity]();
        strcpy(_data, str);
    }

    ~CharList(){
        if(_data != nullptr){
            delete[] _data;
            _data = nullptr;
        }
    }

    /**
     * 重载[]运算符，需要判断越界
     * @param idx
     * @return
     */
    const char& operator[](size_t idx) const{
        if (idx < _capacity - 1 && idx > -1) {
            /**
             * 可以改data内容，但是不能改指向
             */
            return _data[idx];
        }

        cout << "out of range" << endl;
        static char nullchar = '\0';
        return nullchar;
    }
};



void test0(){
    CharList list("Goodbye!world!");

//    list[8] = '0';
    cout << list[8] <<endl;

    list[100];
}

void test1(){
    const CharList list("Goodbye");
    cout << list[2] << endl;

}

int main() {
    test0();
    return 0;
}